// pages/indexList/indexList.js
const req = require('../../utils/apifm-wxapi')
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      slides:[],
      basicInfo:{},
      token:wx.getStorageSync('Token')||'',
      id:'',
      flag:false,
      show:false,
      number:'',
      cs:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id)
    req.goodsDetail(options.id).then((res)=>{
      console.log(res.data)
       WxParse.wxParse('article', 'html', res.data.content, this, 5);
      this.setData({
        slides:res.data.pics,
        basicInfo:res.data.basicInfo,
        id:options.id
      })
    })
    var Token=this.data.token
    var id =options.id
    console.log(Token)
  console.log(id)
    req.goodsFavCheck(Token,id).then((res)=>{
      console.log(res)
      if(res.code==0){
        this.setData({
          flag:true
        })
      }else{
        this.setData({
          flag:false
        })
      }
    })
  },
shou(){
  var Token=this.data.token
  var id =this.data.id
  console.log(Token)
  console.log(id)
  var flag =!this.data.flag
  if(flag){
    req.goodsFavPut(Token,id).then((res)=>{
      console.log(res)
    })
    req.goodsFavCheck(Token,id).then((res)=>{
      console.log(res)
      if(res.code==0){
        wx.showToast({
          title: '收藏成功',
        })
        wx.setStorageSync('flag', flag)
      }
    })
    this.setData({
      flag:flag
    })
  }else{
    req.goodsFavDelete(Token,id).then((res)=>{
      console.log(res)
    })
    req.goodsFavCheck(Token,id).then((res)=>{
      console.log(res)
      if(res.code==0){
        wx.showToast({
          title: '取消收藏',
        })
        wx.setStorageSync('flag', flag)
      }
    })
    this.setData({
      flag:flag
    })
  }
},
show(){
  var show=true
  this.setData({
    show:show
  })
  console.log(this.data.show)
},
onClose() {
  var show=false
  this.setData({ show: show,cs:false });
},
onChange(event) {
  console.log(event.detail);
  this.setData({
    number:event.detail
  })
},
// 加入购物车
add(){
  var number=this.data.number
  var Token =wx.getStorageSync('Token')
  var goodsId =this.data.basicInfo.id
  req.goodsCartadd(Token,goodsId,number).then((res)=>{
    console.log(res)
    if(res.code==0){
      wx.showLoading({})
      wx.setTabBarBadge({
        index: 2,
        text: String(res.data.number),
      })
      wx.setStorageSync('number', res.data.number)
      var show=false
    this.setData({ show: show });
    wx.hideLoading()
    }
  })
},
cart(){
  wx.switchTab({
    url: '/pages/shopping/shopping',
  })
},
// 二维码分享
share(){
  var obj={
    scene:wx.getStorageSync('uid'),
    is_hyaline:true,
    autoColor:true,
    expireHours:1
  }
  req.wxaQrcode(obj).then((res)=>{
    console.log(res)
    if(res.code !=0){
      wx.showToast({
        title: res.msg,
        icon:"none"
      })
      return
    }
    this.setData({
      cs:true,
      erm:res
    })
    console.log(this.data.erm)
  })
},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var Token = wx.getStorageSync('Token')
    this.setData({
      token:Token
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})